define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	var Controller = {
		index  : function () {
			// 初始化表格参数配置
			Table.api.init({
				extend : {
					index_url  : 'qingdongams/jigoucustomer/customer_product/index',
					detail_url : 'qingdongams/jigoucustomer/customer_product/detail',
					table      : 'customer_product',
				}
			});

			var table = $("#table");
			var q = {};
			location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v);
			var isselect=false;
			if (q.isselect== 1) {
				isselect= true;
			}
			// 初始化表格
			table.bootstrapTable({
				url          : $.fn.bootstrapTable.defaults.extend.index_url,
				pk           : 'id',
				sortName     : 'id',
				fixedColumns : true,
				fixedNumber  : 2,
				search:false,
				searchFormVisible:true,
				columns      : [
					[
						{checkbox : true},
						{
							field : 'customer.name', title : '客户名称', fixedColumns : true, formatter : function (value, row, index) {
								if(!value){
									return "<a href='javascript:void(0);'>--</a>";
								}

								return "<a href='javascript:void(0);' data-id='" + row.customer.id + "' class='show-customer-detail'>" + value + "</a>";
							},operate:false
						},
						{field : 'customer_id', title : '客户名称', visible: false, addClass: "selectpage", extend: "data-source='qingdongams/jigoucustomer/customer/index' data-field='name'"},
						{
							field : 'contracts.num', title : '关联合同', fixedColumns : true, formatter : function (value, row, index) {
								if(!value){
									return "<a href='javascript:void(0);' >--</a>";
								}
								return "<a href='javascript:void(0);' data-id='" + row.contracts.id + "' class='show-contracts-detail'>" + value + "</a>";
							},operate:false
						},
						{
							field : 'product.name', title : '产品名称', fixedColumns : true, formatter : function (value, row, index) {
								if(!value){
									return "<a href='javascript:void(0);' >产品已删除</a>";
								}
								return "<a href='javascript:void(0);' data-id='" + row.id + "' class='show-detail'>" + value + "</a>";
							},operate:false
						},
						{field : 'product.unit', title : '产品单位',operate:false},
						{field : 'product.img', title : '产品图', formatter: Table.api.formatter.image,operate:false},
						{field : 'status', title : '产品状态', formatter : Table.api.formatter.status,
							searchList : {0 : '待发货', 1 : '发货中', 2 : '待安装', 3 : '安装成功',4:'旧机维修',9:'已取消'}},
						{field : 'number', title : '绑定设备编号'},
						{field : 'product.num', title : '产品编码',operate:false},
						{field : 'product.price', title : '标准价格',operate:false},
						{field : 'product.unit', title : '单位',operate:false},
						{field : 'pay_date', title : '购买时间', operate: '=', addclass: 'datetimepicker',data:"data-date-format=\" YYYY-MM-DD\""},
						{
							field: 'operate2', title: __('Operate'),visible:isselect, events: {
								'click .btn-chooseone': function (e, value, row, index) {
									Fast.api.close(row);
								},
							}, formatter: function () {
								return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
							}
						},

					]
				]
			});

			$(document).on('click', '.show-detail', function (data) {
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose : false,
					shade      : [0.3, '#393D49'],
					area       : area,
					callback   : function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						console.log(value);
					}
				};
				Fast.api.open($.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + $(this).data('id'), '客户产品详情', options);
			});
			$(document).on('click', '.show-customer-detail', function (data) {
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose : false,
					shade      : [0.3, '#393D49'],
					area       : area,
					callback   : function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						console.log(value);
					}
				};
				Fast.api.open("qingdongams/jigoucustomer/customer/detail?ids=" + $(this).data('id'), '客户详情', options);
			}).on('click', '.show-contracts-detail', function (data) {
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose : false,
					shade      : [0.3, '#393D49'],
					area       : area,
				};
				Fast.api.open("qingdongams/jigoucustomer/contracts/detail?ids=" + $(this).data('id'), '合同详情', options);
			});
			// 为表格绑定事件
			Table.api.bindevent(table);
		},
		add    : function () {
			Controller.api.bindevent();
		},
		add_quipment    : function () {
			Controller.api.bindevent();
		},
		edit   : function () {


			Controller.api.bindevent();
		},
		detail : function () {
			Table.api.init({});
			var table = $("#table-workorder");
			// 初始化表格
			table.bootstrapTable({
				url          : 'qingdongams/jigoucustomer/customer_product/get_workorder' + location.search,
				pk           : 'id',
				sortName     : 'id',
				operate      : false,
				commonSearch : false,
				search       : false,
				visible      : false,
				showColumns  : false,
				showExport   : false,
				showToggle   : false,
				columns      : [
					[
						{field : 'workorder_number', title : '工单编号', formatter: function (value, row, index) {

								return "<a href='javascript:void(0);' data-id='" + row.id + "' class='show-workorder-detail'>" + value + "</a>";
							}},
						{field : 'title', title : '工单标题'},
						{field : 'workorder_type', title : '工单类型'},
						{field : 'status', title : '工单状态', formatter : Table.api.formatter.status,
							searchList : {0 : '待分配', 1 : '待处理', 2 : '处理中', 3 : '已完成',9:'已取消'}},
						{field : 'process', title : '工单进程', formatter : Table.api.formatter.status,
							searchList : {0 : '已提交', 1 : '备货中', 2 : '备货完成', 3 : '到访/录入物流信息',8:'等待物流发货',7:'费用待审核', 9 : '已送达'}},
						{field : 'createtime', title : '创建时间', formatter: Table.api.formatter.datetime},
						{field : 'end_time', title : '结束时间', formatter: Table.api.formatter.datetime},
						{field : 'results', title : '结果说明'},
					]
				]
			});
			$(".btn-downloaderweima").on("click",function() {
				var temp = table.bootstrapTable('getSelections');
				var ids = temp.map(item => item.id);
				ids = ids.toString();
				if (!ids) {
					Layer.msg("请选择要下载的二维码");
					return false;
				}
				var curl = 'export_ercode?ids=' + ids;
				window.location.href = curl;
			});
			$(document).on('click', '.show-workorder-detail', function () {
				var id = $(this).data('id');
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose: false,
					shade: [0.3, '#393D49'],
					area: area,
				};
				Fast.api.open("qingdongams/workorder/workorder/detail?ids=" + id, '工单详情', options);
			})
					//编辑
			$(document).on('click', '.btn-edit', function () {
				var id = $('#ids').val();
				Fast.api.open("qingdongams/jigoucustomer/product/edit?ids=" + id, "产品编辑", {
					shadeClose : false,
					shade      : false,
					// maxmin     : false,
					moveOut    : false,
					scrollbars : false,
					callback   : function () {
						location.reload()
					}
				});

			}).on('click', ".btn-del", function () {//删除
				var id = $('#ids').val();
				Layer.confirm('确定删除当前产品吗？', {
					btn   : ['确定', '取消'],
					title : '提示',
				}, function (index, layero) {
					Fast.api.ajax("qingdongams/jigoucustomer/product/del?ids=" + id, function (data, ret) {
						if (ret.code == 1) {
							Layer.close(index);
							parent.location.reload();
						}
					}, function (data, ret) {
					});

				});
			});
			Controller.api.bindevent();
		},
		api    : {
			bindevent : function () {
				Form.api.bindevent($("form[role=form]"));
			}
		}
	};
	return Controller;
});